package g4p.newg4p;

import g4p_controls.G4P;
import g4p_controls.GAbstractControl;
import g4p_controls.GButton;
import g4p_controls.GCheckbox;
import g4p_controls.GControlMode;
import g4p_controls.GEvent;
import g4p_controls.GKnob;
import g4p_controls.GPanel;
import g4p_controls.GTextArea;
import g4p_controls.GTextField;
import g4p_controls.GValueControl;

import java.awt.event.KeyEvent;

import processing.core.PApplet;
import processing.core.PGraphicsJava2D;

public class Panel_Test extends PApplet{
	
	GPanel pnlSample;
	GTextArea txaSample;
	GTextField txfSample;
	GButton btnSample;
	
	GKnob knbAngle;
	GButton[] btnColours = new GButton[8];
	GCheckbox cbxJustify, cbxEnabled;
	
	PGraphicsJava2D snapshot = null;
	
	String s;
	
	public void setup() {
		size(900,600);
		cursor(CROSS);
		
		G4P.setCtrlMode(GControlMode.CORNER);
		
		for(int i = 0; i < btnColours.length; i++){
			btnColours[i] = new GButton(this, width - 34, 4 + i * 22, 30, 18, "" + (i+1));
//			btnColours[i].setLocalColorScheme(i);
			btnColours[i].tagNo = 1000 + i;
		}

		knbAngle = new GKnob(this, width - 90, height -  100, 70, 70, 4);
		knbAngle.setLimits(0, TWO_PI);
		knbAngle.setValue(0);
		
		String[] paragraphs = loadStrings("g4p/book2.txt");
		if(paragraphs.length > 0)
			s= PApplet.join(paragraphs, '\n');
		else
			s = paragraphs[0];

		//                            x   y   w   h
		pnlSample = new GPanel(this, 30, 40, 290, 300, "Peter's Panel");
		pnlSample.setCollapsed(false);
		pnlSample.setDragArea(100,0,400,500);
		
//		G4P.setCtrlMode(GControlMode.CORNER);
//		btnSample = new GButton(this, 0, 0, 60, 50, "Sample button face text");
//		btnSample.tag = "Button";
//		pnlSample.addControl(btnSample, 220, 30, radians(0 ));
//
//		G4P.setCtrlMode(GControlMode.CORNER);
//		txaSample = new GTextArea(this, 10, 30, 200, 200, G4P.SCROLLBARS_BOTH | G4P.SCROLLBARS_AUTOHIDE);
//		txaSample.tag = "TextArea";
//		pnlSample.addControl(txaSample, 10, 30, radians(0));


		G4P.setCtrlMode(GControlMode.CENTER);
		btnSample = new GButton(this, 0, 0, 60, 50, "Sample button face text");
		btnSample.tag = "Button";
		pnlSample.addControl(btnSample, 250, 55, radians(0));

		G4P.setCtrlMode(GControlMode.CENTER);
		txaSample = new GTextArea(this, 100, 100, 200, 200, G4P.SCROLLBARS_BOTH | G4P.SCROLLBARS_AUTOHIDE);
		txaSample.tag = "TextArea";
		pnlSample.addControl(txaSample, 110, 130, radians(0));

//		printControl(txaSample);
		txaSample.setText(s, 330);

	}

	public void draw() {
		background(200);
		fill(0);
		//c0.draw();
		text("["+mouseX+","+mouseY+"]", 20,20);
		text("["+mouseX+","+mouseY+"]", width - 100,height - 10);
		if(snapshot != null){
			image(snapshot, 0, 0);
			pnlSample.setVisible(false);
		}
	}

	public void printControl(GAbstractControl control){
		System.out.println(control.tag + "  [" + control.getX() + ", " + control.getY() +"]" + "  [" + control.getCX() + ", " + control.getCY() +"]");
	}
	
	public void keyReleased(KeyEvent e){
		if(key == ' '){
			snapshot = (PGraphicsJava2D) pnlSample.getSnapshot();
		}
		if(key == 'o' || key == 'O'){
			pnlSample.setOpaque(!pnlSample.isOpaque());
		}
	}
	
	public void handleKnobEvents(GValueControl knob, GEvent event) {
		pnlSample.setRotation(knbAngle.getValueF());
	}

	public void handleButtonEvents(GButton button, GEvent event) { 
		if(button.tagNo >= 1000)
			pnlSample.setLocalColorScheme(button.tagNo - 1000);
	}

}
